﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class UIReward : MonoBehaviour
{
    Transform top, star, starBG, bg2, reward1, reward2, coin;
    Transform[] items = new Transform[5];

    private void Awake()
    {
        top = transform.Find("Top");
        star = transform.Find("Stars");
        starBG = transform.Find("Sun_Shines_01");
        coin = transform.Find("Coin_01");
        bg2 = transform.Find("BG2");
        reward1 = transform.Find("Reward1");
        reward2 = transform.Find("Reward2");
        
        for (int i = 0; i < 5; i++)
            items[i] = transform.Find("Items").GetChild(i);
        UGUIEventListener.Get(reward1.gameObject).onClick = (obj) =>
        {
            Time.timeScale = 1;
            if (SceneManager.GetActiveScene().buildIndex == 2)
                SceneManager.LoadScene("LV01");
            else
                SceneManager.LoadScene("LV0" + (SceneManager.GetActiveScene().buildIndex + 2).ToString());
        };
        UGUIEventListener.Get(reward2.gameObject).onClick = (obj) =>
        {
            Time.timeScale = 1;
            if (SceneManager.GetActiveScene().buildIndex == 2)
                SceneManager.LoadScene("LV01");
            else
                SceneManager.LoadScene("LV0" + (SceneManager.GetActiveScene().buildIndex + 2).ToString());
        };
    }

    private void Start()
    {
        HideAll();
    }

    public void Open()
    {
        StartCoroutine(IEOpen());
    }

    IEnumerator IEOpen()
    {
        top.GetComponent<TweenAlpha>().enabled = true;
        top.GetComponent<TweenAlpha>().PlayForward();
        yield return new WaitForSeconds(0.3f);
        starBG.gameObject.SetActive(true);
        starBG.GetComponent<ParticleSystem>().Play();
        for (int i = 0; i < 3; i++)
        {
            star.GetChild(i).gameObject.SetActive(true);
            star.GetChild(i).GetComponent<ParticleSystem>().Play();
            yield return new WaitForSeconds(0.15f);
        }
        bg2.gameObject.SetActive(true);
        for (int i = 0; i < 5; i++)
        {
            items[i].gameObject.SetActive(true);
            yield return new WaitForSeconds(0.1f);
        }
        coin.gameObject.SetActive(true);
        coin.GetComponent<ParticleSystem>().Play();
        reward1.gameObject.SetActive(true);
        reward2.gameObject.SetActive(true);
        //Time.timeScale = 0;
    }

    void HideAll()
    {
        for (int i = 0; i < 3; i++)
            star.GetChild(i).gameObject.SetActive(false);
        starBG.gameObject.SetActive(false);
        bg2.gameObject.SetActive(false);
        for (int i = 0; i < 5; i++)
            items[i].gameObject.SetActive(false);
        coin.gameObject.SetActive(false);
        reward1.gameObject.SetActive(false);
        reward2.gameObject.SetActive(false);
    }
}